package com.css.fxfzypg.modules.rating.task.dao;

import com.css.fxfzypg.modules.rating.task.model.entity.KpRatingTaskArea;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Repository
public class KpRatingTaskAreaRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public void batchSave(List<KpRatingTaskArea> areaList) {
        String sql = "INSERT INTO kp_rating_task_area(id, province_id, krtyt_id, area, area_ratio) values(:id, :provinceId, :krtytId, :area, :areaRatio)";
        namedParameterJdbcTemplate.batchUpdate(sql, SqlParameterSourceUtils.createBatch(areaList.toArray()));
    }

    public List<KpRatingTaskArea> list(KpRatingTaskArea kpRatingTaskArea) {
        StringBuilder sql = new StringBuilder("select * from kp_rating_task_area where 1=1 ");
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getId())) {
            sql.append(" and id = '").append(kpRatingTaskArea.getId()).append("' ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getProvinceId())) {
            sql.append(" and province_id = '").append(kpRatingTaskArea.getProvinceId()).append("' ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getKrtytId())) {
            sql.append(" and krtyt_id = '").append(kpRatingTaskArea.getKrtytId()).append("' ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getArea())) {
            sql.append(" and area = '").append(kpRatingTaskArea.getArea()).append("' ");
        }
        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(KpRatingTaskArea.class));
    }

    public List<KpRatingTaskArea> selectByKrtId(String krtId) {
        String sql = "select t1.* from kp_rating_task_area t1 left join kp_rating_task_ypg_task t2 on t1.krtyt_id = t2.id where t2.krt_id = '" + krtId + "'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KpRatingTaskArea.class));
    }

    public List<KpRatingTaskArea> getByProvinceIdAndKryId(String provinceId, String krtId) {
        String sql = "select * from kp_rating_task_area t1 left join kp_rating_task_ypg_task t2 on t1.krtyt_id = t2.id where t2.krt_id = '" + krtId + "' and t1.province_id = '" + provinceId + "'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KpRatingTaskArea.class));
    }

    public void updateById(KpRatingTaskArea kpRatingTaskArea) {
        StringBuilder sql = new StringBuilder("update kp_rating_task_area set ");
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getId())) {
            sql.append(" id = '").append(kpRatingTaskArea.getId()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getProvinceId())) {
            sql.append(" province_id = '").append(kpRatingTaskArea.getProvinceId()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getKrtytId())) {
            sql.append(" krtyt_id = '").append(kpRatingTaskArea.getKrtytId()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getArea())) {
            sql.append(" area = '").append(kpRatingTaskArea.getArea()).append("', ");
        }
        if (ObjectUtils.isNotEmpty(kpRatingTaskArea.getAreaRatio())) {
            sql.append(" area_ratio = '").append(kpRatingTaskArea.getAreaRatio()).append("', ");
        }
        sql.deleteCharAt(sql.lastIndexOf(", "));
        sql.append(" where id = '").append(kpRatingTaskArea.getId()).append("' ");
        jdbcTemplate.update(sql.toString());
    }

    public List<KpRatingTaskArea> getByKrtId(String krtId) {
        String sql = "select DISTINCT t1.* from kp_rating_task_area t1 left join kp_rating_task_ypg_task t2 on t1.krtyt_id = t2.id where t2.krt_id = '" + krtId + "'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KpRatingTaskArea.class));
    }

    public List<KpRatingTaskArea> getByKrtytId(String krtytId) {
        String sql = "select * from kp_rating_task_area where krtyt_id = '" + krtytId + "'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KpRatingTaskArea.class));
    }

    public List<KpRatingTaskArea> getByKrtIdAndProvinceId(String krtId, String provinceId) {
        String sql = String.format("select DISTINCT t1.* from kp_rating_task_area t1 left join kp_rating_task_ypg_task t2 on t1.krtyt_id = t2.id where t2.krt_id = '%s' and t1.province_id = '%s'", krtId, provinceId);
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KpRatingTaskArea.class));
    }

    public BigDecimal getAreaRatioByKrtytId(String krtytId) {
        String sql="select area_ratio from kp_rating_task_area where krtyt_id='"+krtytId+"'";
        try {
           return jdbcTemplate.queryForObject(sql,BigDecimal.class);
        }catch (Exception e){
            return BigDecimal.ONE;
        }
    }
}
